using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using System.Threading;
namespace MicroMsg.Storage
{
	public class TestStorage
	{
		public class testSpeedDB : DataContext
		{
			public Table<Contact> contact;
			public testSpeedDB() : base("Data Source=isostore:/storage/testSpeed.sdf;")
			{
				if (!base.DatabaseExists())
				{
					base.CreateDatabase();
				}
				base.SubmitChanges();
			}
		}
		[Table]
		public class TestTable1
		{
			[Column(IsPrimaryKey = true)]
			public string strData;
		}
		[Table]
		public class TestTable2
		{
			[Column(IsPrimaryKey = true)]
			public string strData;
		}
		[Table]
		public class TestTable3
		{
			[Column(IsPrimaryKey = true)]
			public string strData;
		}
		public class testDataContext : DataContext
		{
			public Table<TestStorage.TestTable1> table1;
			public Table<TestStorage.TestTable2> table2;
			public Table<TestStorage.TestTable3> table3;
			public testDataContext() : base("Data Source=isostore:/test.sdf;Password='securepassword';")
			{
				if (base.DatabaseExists())
				{
					base.DeleteDatabase();
				}
				base.CreateDatabase();
				base.SubmitChanges();
			}
			public void method_0()
			{
				Thread thread = new Thread(new ParameterizedThreadStart(TestStorage.testDataContext.threadLoop1));
				thread.set_Name("Thread1");
				thread.set_IsBackground(true);
				thread.Start(this);
				thread = new Thread(new ParameterizedThreadStart(TestStorage.testDataContext.threadLoop2));
				thread.set_Name("Thread2");
				thread.set_IsBackground(true);
				thread.Start(this);
				thread = new Thread(new ParameterizedThreadStart(TestStorage.testDataContext.threadLoop3));
				thread.set_Name("Thread3");
				thread.set_IsBackground(true);
				thread.Start(this);
			}
			private static void threadLoop1(object param)
			{
				TestStorage.testDataContext testDataContext = param as TestStorage.testDataContext;
				int num = 0;
				while (true)
				{
					string strData = "111____" + num;
					num++;
					testDataContext.table1.InsertOnSubmit(new TestStorage.TestTable1
					{
						strData = strData
					});
					testDataContext.SubmitChanges();
					if (Queryable.Count<TestStorage.TestTable1>(testDataContext.table1) >= 100)
					{
						while (Queryable.Count<TestStorage.TestTable1>(testDataContext.table1) > 10)
						{
							testDataContext.table1.DeleteOnSubmit(Queryable.First<TestStorage.TestTable1>(testDataContext.table1));
							testDataContext.SubmitChanges();
						}
					}
				}
			}
			private static void threadLoop2(object param)
			{
				TestStorage.testDataContext testDataContext = param as TestStorage.testDataContext;
				int num = 0;
				while (true)
				{
					"222____" + num;
					num++;
					testDataContext.table2.InsertOnSubmit(new TestStorage.TestTable2
					{
						strData = "222"
					});
					testDataContext.SubmitChanges();
					if (Queryable.Count<TestStorage.TestTable2>(testDataContext.table2) >= 100)
					{
						while (Queryable.Count<TestStorage.TestTable2>(testDataContext.table2) > 10)
						{
							testDataContext.table2.DeleteOnSubmit(Queryable.First<TestStorage.TestTable2>(testDataContext.table2));
							testDataContext.SubmitChanges();
						}
					}
				}
			}
			private static void threadLoop3(object param)
			{
				TestStorage.testDataContext testDataContext = param as TestStorage.testDataContext;
				int num = 0;
				while (true)
				{
					string strData = "333____" + num;
					num++;
					testDataContext.table3.InsertOnSubmit(new TestStorage.TestTable3
					{
						strData = strData
					});
					testDataContext.SubmitChanges();
					if (Queryable.Count<TestStorage.TestTable3>(testDataContext.table3) >= 100)
					{
						while (Queryable.Count<TestStorage.TestTable3>(testDataContext.table3) > 10)
						{
							testDataContext.table3.DeleteOnSubmit(Queryable.First<TestStorage.TestTable3>(testDataContext.table3));
							testDataContext.SubmitChanges();
						}
					}
				}
			}
		}
		private const string TAG = "TestStorage";
		private static StringBuilder sStrBufer = new StringBuilder(524288);
		public static List<string> strList = new List<string>();
		public static void testSaveData(List<Contact> contactList)
		{
			TestContactData testContactData = new TestContactData();
			testContactData.contactList = contactList;
			Log.smethod_5("TestStorage", "write xml begin");
			StorageXml.saveToFile<TestContactData>(testContactData, "contact_list.xml");
			Log.smethod_5("TestStorage", "write xml end");
		}
		public static void testDigData()
		{
		}
		public static void testTemplate()
		{
		}
		public static void testMultipleInstances()
		{
			using (TestStorage.testSpeedDB testSpeedDB = new TestStorage.testSpeedDB())
			{
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "1111",
					strNickName = "1111"
				});
				testSpeedDB.SubmitChanges();
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "2222",
					strNickName = "2222"
				});
				testSpeedDB.SubmitChanges();
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "3333",
					strNickName = "3333"
				});
				testSpeedDB.SubmitChanges();
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "4444",
					strNickName = "4444"
				});
				testSpeedDB.SubmitChanges();
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "5555",
					strNickName = "5555"
				});
				testSpeedDB.SubmitChanges();
				testSpeedDB.contact.InsertOnSubmit(new Contact
				{
					strUsrName = "6666",
					strNickName = "6666"
				});
				testSpeedDB.SubmitChanges();
			}
			using (TestStorage.testSpeedDB testSpeedDB2 = new TestStorage.testSpeedDB())
			{
				testSpeedDB2.set_ObjectTrackingEnabled(false);
				Queryable.First<Contact>(testSpeedDB2.contact);
				using (TestStorage.testSpeedDB testSpeedDB3 = new TestStorage.testSpeedDB())
				{
					Contact contact = Queryable.First<Contact>(testSpeedDB3.contact);
					contact.strNickName = "@@@@";
					testSpeedDB3.SubmitChanges();
				}
				Queryable.First<Contact>(testSpeedDB2.contact);
			}
		}
		public static void TestAttach()
		{
			Contact contact;
			using (TestStorage.testSpeedDB testSpeedDB = new TestStorage.testSpeedDB())
			{
				contact = new Contact
				{
					strUsrName = "ball",
					strNickName = "222"
				};
				testSpeedDB.contact.InsertOnSubmit(contact);
				testSpeedDB.SubmitChanges();
			}
			contact.strNickName = "1111";
			contact.strMobile = "333";
			using (TestStorage.testSpeedDB testSpeedDB2 = new TestStorage.testSpeedDB())
			{
				testSpeedDB2.contact.Attach(contact);
				testSpeedDB2.Refresh(1, contact);
				testSpeedDB2.contact.DeleteOnSubmit(contact);
				testSpeedDB2.SubmitChanges();
				int num = Queryable.Count<Contact>(testSpeedDB2.contact);
				num++;
			}
			using (TestStorage.testSpeedDB testSpeedDB3 = new TestStorage.testSpeedDB())
			{
				Queryable.First<Contact>(testSpeedDB3.contact);
				testSpeedDB3.SubmitChanges();
			}
		}
		public static void TestSpeed()
		{
			TestStorage.testSpeedDB testSpeedDB = new TestStorage.testSpeedDB();
			int num = 0;
			new Random();
			long arg_1B_0 = DateTime.get_Now().get_Ticks();
			new List<Contact>();
			for (int i = 0; i < 10000; i++)
			{
				Contact contact = new Contact();
				contact.strUsrName = "user_" + i;
				contact.strNickName = "nickname111" + i;
				contact.strMobile = "112121222222";
				contact.strEmail = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
				contact.strHeadImg = "abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz";
				try
				{
					testSpeedDB.contact.InsertOnSubmit(contact);
				}
				catch
				{
					testSpeedDB.contact.DeleteOnSubmit(contact);
					num++;
				}
			}
			long arg_C2_0 = DateTime.get_Now().get_Ticks();
			testSpeedDB.SubmitChanges();
			for (int j = 0; j < 3; j++)
			{
				Log.smethod_5("testspeed", "start " + j);
				using (TestStorage.testSpeedDB testSpeedDB2 = new TestStorage.testSpeedDB())
				{
					Log.smethod_5("testspeed", "open db");
					IQueryable<Contact> queryable = Queryable.Select<Contact, Contact>(testSpeedDB2.contact, (Contact c) => c);
					Enumerable.ToList<Contact>(queryable);
					string selUser = "user_500" + j;
					IQueryable<Contact> queryable2 = Queryable.Where<Contact>(testSpeedDB2.contact, (Contact c) => c.strUsrName == selUser);
					Contact contact2 = Queryable.First<Contact>(queryable2);
					testSpeedDB2.contact.DeleteOnSubmit(contact2);
					Log.smethod_5("testspeed", "before submit");
					testSpeedDB2.SubmitChanges();
					Log.smethod_5("testspeed", "after submit");
					Log.smethod_5("testspeed", "db close");
				}
			}
			Log.smethod_5("testspeed", "start db2");
			using (TestStorage.testSpeedDB testSpeedDB3 = new TestStorage.testSpeedDB())
			{
				Contact contact3 = new Contact
				{
					strUsrName = "adasdfasdf"
				};
				testSpeedDB3.contact.InsertOnSubmit(contact3);
				testSpeedDB3.SubmitChanges();
			}
			Log.smethod_5("testspeed", "start db3");
		}
		private static void logOutputTest(object param)
		{
			int num = 0;
			int num2 = (int)param;
			while (true)
			{
				Log.smethod_5("TestLogThread", string.Concat(new object[]
				{
					"log thread=",
					num2,
					",count=",
					num++
				}));
			}
		}
		public static void TestLogThread()
		{
			for (int i = 0; i < 10; i++)
			{
				Thread thread = new Thread(new ParameterizedThreadStart(TestStorage.logOutputTest));
				thread.Start(i);
			}
		}
		public static void TestParallel()
		{
			StorageBasic.onLogin("test");
			Random seed = new Random();
			StorageTable<ChatMsg, int> store = new StorageTable<ChatMsg, int>((ChatMsg m) => m.nMsgLocalID);
			ThreadStart threadStart = delegate
			{
				int num = 0;
				do
				{
					Random seed;
					int nMsgLocalID;
					lock (seed)
					{
						nMsgLocalID = seed.Next() + 10000;
					}
					store.method_3(new ChatMsg
					{
						nMsgLocalID = nMsgLocalID,
						strMsg = Thread.get_CurrentThread().get_Name() + "_item_" + num
					});
					num++;
					Log.smethod_5("test", Thread.get_CurrentThread().get_Name() + " count=" + num);
				}
				while (num <= 1000);
				Log.smethod_5("test", "add msg fiinished! " + Thread.get_CurrentThread().get_Name());
			};
			Thread thread;
			for (int i = 0; i < 3; i++)
			{
				thread = new Thread(threadStart);
				thread.set_Name("Thread" + i);
				thread.set_IsBackground(true);
				thread.Start();
			}
			thread = new Thread(new ParameterizedThreadStart(TestStorage.add10000Item));
			thread.set_Name("Thread 10000");
			thread.set_IsBackground(true);
			thread.Start(store);
			thread.Join();
			store.dumpTable();
		}
		public static void add10000Item(object param)
		{
			StorageTable<ChatMsg, int> storageTable = param as StorageTable<ChatMsg, int>;
			List<ChatMsg> list = new List<ChatMsg>();
			for (int i = 0; i < 10000; i++)
			{
				list.Add(new ChatMsg
				{
					nMsgLocalID = i,
					strTalker = "test",
					nMsgSvrID = i,
					nMsgType = 1,
					strMsg = "text_" + i
				});
			}
			DebugEx.getTimeSpan();
			storageTable.addList(list);
			Log.smethod_5("test", "add  10000 msg fiinished! " + Thread.get_CurrentThread().get_Name());
		}
		public static void TestThreadSafe()
		{
			TestStorage.testDataContext testDataContext = new TestStorage.testDataContext();
			testDataContext.method_0();
		}
	}
}
